<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Victor
 * Date: 23.11.11
 * Time: 20:54
 * To change this template use File | Settings | File Templates.
 */

class Role_Manager {


    private static $instance = null;
    /**
     * @var DataModule
     */
    private $dataModule;


    public function setDataModule(DataModule $dataModule)
    {
        $this->dataModule = $dataModule;

    }

    private function __construct()
    {

    }

    /**
     * @static
     * @return Role_Manager
     */
    public static function getInstance()
    {
        if (self::$instance == null)
            self::$instance = new Role_Manager();

        //echo "RoleManager class created ...</br>";
        return self::$instance;

    }

    public function addRole(Role_Role $role)
    {
        $log = $this->logger;
         $log->info('Start indexAction in IndexController</br>');
        //echo "Call addRole ..";
        $pdo = $this->dataModule->getConnection();

        $transaction = true;
        $roleName = $role->getRoleName();
        //echo "DEBUG: rolename: " . $roleName . "</br>";
        if (strlen($roleName) == 0 && !isset($roleName)) {
            $transaction = false;
        }
        if ($transaction) {
            try {
                //echo "Begin Transaction</br>";
                $pdo->beginTransaction();
                $sth = $pdo->prepare("INSERT INTO `dzundza_role`(roleName)  VALUES (:roleName)");
                $sth->bindParam(':roleName', $roleName, PDO::PARAM_STR, 45);
                $sth->execute();
                //echo "Execute Insert</br>";
                $pdo->commit();
                //echo "Commit query </br>";
                $this->dataModule->closeConnection();
            } catch (PDOException $e) {

                //echo "Rollback Query ...";
                $pdo->rollback();
                die("Error: " . $e->getMessage() . "</br>");
            }
        }
    }

    /**
     * @return array
     */
    public function getAllRoles()
    {
        try {
            $pdo = $this->dataModule->getConnection();
           // if (isset($pdo)) echo "Connected:</br>";
            $sql = "SELECT idRole,roleName FROM `dzundza_role`;";

            $result = $pdo->query($sql);
            $roles = $result->fetchAll(PDO::FETCH_ASSOC);
            

            return $roles;

        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    /**
     * @return array
     */
    public function checkRole($roleName)
    {
        //var_dump($roleName);
        try {
                $isRole = false;
            $pdo = $this->dataModule->getConnection();



            // if (isset($pdo)) echo "Connected:</br>";
            $sql = "SELECT count(roleName) as cn FROM `dzundza_role` WHERE roleName = :name ;";
             $stn = $pdo->prepare($sql);
            $stn->bindValue(":name",$roleName,PDO::PARAM_STR);
            $stn->execute();
            $result=$stn->fetch();
            
            if($result['cn'] == 0 ) $isRole = false; else
                $isRole =  true;

            return $isRole;

        } catch (PDOException $e) {
            echo $e->getMessage();
        }

    }
}
